using B.S.BaseData.Api.Read.Command.OrderCommand;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interface;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using WorkOrder.Domain;
namespace B.S.BaseData.Api.Read.CommandHandler.OrderCommandHandler
{
    /// <summary>
    /// 药品订单处理器
    /// </summary>
    public class MedicineOrderHandler : IRequestHandler<MedicineOrderQueryCommand, APIResult<ApiPaging<MedicineOrder>>>
    {

        private readonly IBaseRepository<MedicineOrder> order;

        public MedicineOrderHandler(IBaseRepository<MedicineOrder> order)
        {
            this.order = order;
        }



        /// <summary>
        /// 处理药品订单查询命令
        /// </summary>
        /// <param name="request">查询命令</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>分页结果</returns>
        public  Task<APIResult<ApiPaging<MedicineOrder>>> Handle(MedicineOrderQueryCommand request, CancellationToken cancellationToken)
        {
           
                APIResult<ApiPaging<MedicineOrder>>result=new APIResult<ApiPaging<MedicineOrder>>();
                // 构建查询
                var query = order.GetAll();

                // 按订单状态筛选
                if (!string.IsNullOrEmpty(request.OrderStatus))
                {
                    query = query.Where(o => o.OrderStatus == request.OrderStatus);
                    Console.WriteLine($"按状态筛选: {request.OrderStatus}, 筛选后数量: {query.Count()}");
                }

                if (!string.IsNullOrEmpty(request.SearchKeyword))
                {
                    query = query.Where(o => o.OrderNumber.Contains(request.SearchKeyword) || o.UserName.Contains(request.SearchKeyword));
                    Console.WriteLine($"按关键词筛选: {request.SearchKeyword}, 筛选后数量: {query.Count()}");
                }

                if (request.StartTime != null && request.EndTime != null)
                {
                    query = query.Where(a => a.SubmitTime >= request.StartTime && a.SubmitTime <= request.EndTime.Value.AddDays(1));
                    Console.WriteLine($"按时间筛选后数量: {query.Count()}");
                }
                // 分页处理
                var totalCount = query.Count();
                var row = query.OrderByDescending(a => a.SubmitTime)
                             .Skip((request.PageIndex - 1) * request.PageSize)
                             .Take(request.PageSize)
                             .ToList();

                Console.WriteLine($"分页后数量: {row.Count()}, PageIndex: {request.PageIndex}, PageSize: {request.PageSize}");

                var totalPage = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize);
                var page = new ApiPaging<MedicineOrder>
                {
                    Total = totalCount,
                    TotalPage = totalPage,
                    PageData = row
                };

                result.Code = ApiEnum.Success;
                result.Msg = "获取药品订单列表成功";
                result.Data = page;
                return Task.FromResult(result);



         
        }
    }
} 